package com.liyujie.dyeingmachine.ui.home.operate.adapter;

import android.annotation.SuppressLint;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.ScreenUtils;
import com.blankj.utilcode.util.SizeUtils;
import com.liyujie.dyeingmachine.R;
import com.liyujie.dyeingmachine.databinding.ItemCylinderBlockBinding;
import com.liyujie.dyeingmachine.listener.UniversalCallback;
import com.liyujie.dyeingmachine.mobile.dao.ReagentEntityDao;
import com.liyujie.dyeingmachine.mobile.entity.CylinderBlockEntity;
import com.liyujie.dyeingmachine.mobile.entity.ReagentEntity;
import com.liyujie.dyeingmachine.views.NoDoubleClickListener;

import java.util.List;

public class CylinderBlockAdapter extends RecyclerView.Adapter<CylinderBlockAdapter.ItemViewHolder> {

    /**
     *
     */
    private List<CylinderBlockEntity> cylinderBlockEntityList;
    private UniversalCallback mUniversalCallback;

    /**
     * 数据设置
     *
     * @param cylinderBlockEntityList
     */
    public void setCylinderBlockEntityList(List<CylinderBlockEntity> cylinderBlockEntityList) {
        this.cylinderBlockEntityList = cylinderBlockEntityList;
        notifyDataSetChanged();
    }

    /**
     * 回调
     *
     * @param mUniversalCallback
     */
    public void setUniversalCallback(UniversalCallback mUniversalCallback) {
        this.mUniversalCallback = mUniversalCallback;
    }


    @NonNull
    @Override
    public ItemViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        ItemCylinderBlockBinding binding = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()), R.layout.item_cylinder_block, parent, false);
        return new ItemViewHolder(binding);
    }


    @Override
    @SuppressLint("RecyclerView")
    public void onBindViewHolder(@NonNull ItemViewHolder holder, int position) {
        CylinderBlockEntity mCylinderBlockEntity = cylinderBlockEntityList.get(position);
        if (null != mCylinderBlockEntity) {
            //编号
            holder.binding.CylinderBlockNumber.setText(mCylinderBlockEntity.CylinderBlockNumber + "");
            //染色中
            if (mCylinderBlockEntity.CylinderDyeing) {
                holder.binding.llCabinet.setBackgroundResource(R.drawable.bg_1_5_c1_0099ff);
                holder.binding.DyeState.setText("染色中");
                holder.binding.CylinderBlockNumber.setTextColor(Color.parseColor("#FFFFFF"));
                holder.binding.DyeState.setTextColor(Color.parseColor("#FFFFFF"));
                holder.binding.ReagentId.setTextColor(Color.parseColor("#FFFFFF"));
            } else {
                holder.binding.llCabinet.setBackgroundResource(R.drawable.bg_1_5_c1_1);
                holder.binding.DyeState.setText("空闲中");
                holder.binding.CylinderBlockNumber.setTextColor(Color.parseColor("#000000"));
                holder.binding.DyeState.setTextColor(Color.parseColor("#000000"));
                holder.binding.ReagentId.setTextColor(Color.parseColor("#000000"));
            }
            //是否绑定
            if (mCylinderBlockEntity.ReagentId > 0) {
                ReagentEntity mReagentEntity = ReagentEntityDao.getInstance().QueryDataByID(mCylinderBlockEntity.ReagentId);
                if (null != mReagentEntity) {
                    holder.binding.ReagentId.setText(mReagentEntity.ReagentName);
                }
            } else {
                holder.binding.ReagentId.setText("没有绑定试剂");
            }

            //点击事件
            holder.binding.llCabinet.setOnClickListener(new NoDoubleClickListener() {
                @Override
                protected void onNoDoubleClick(View v) {
                    if (null != mUniversalCallback) {
                        mUniversalCallback.onUniversalCallback(position, mCylinderBlockEntity);
                    }
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return null == cylinderBlockEntityList ? 0 : cylinderBlockEntityList.size();
    }

    public class ItemViewHolder extends RecyclerView.ViewHolder {
        ItemCylinderBlockBinding binding;

        public ItemViewHolder(@NonNull ItemCylinderBlockBinding binding) {
            super(binding.getRoot());
            this.binding = binding;
            ViewGroup.LayoutParams params = binding.llCabinet.getLayoutParams();
            params.height = (ScreenUtils.getScreenWidth() / 7) + SizeUtils.dp2px(4);
            binding.llCabinet.setLayoutParams(params);
        }
    }
}
